<?php

declare (strict_types=1);

namespace app\admin\model\web;

use app\common\model\web\Feedback as FeedbackModel;
use think\db\exception\DbException;
use think\Paginator;

/**
 * 文章记录
 * Class Article
 * @package app\admin\model\web
 */
class Feedback extends FeedbackModel
{
    /**
     * 获取列表
     * @param array $param
     * @return Paginator
     * @throws DbException
     */
    public function getList($param = [])
    {
        // 查询模型
        $query = $this->getNewQuery();
        // 查询参数
        $filter = $this->getQueryFilter($param);
        // 查询列表记录
        $list = $query
            ->where($filter)
            ->where('is_delete', '=', '0')
            ->order(['create_time' => 'desc'])
            ->paginate($param['pageSize'] ?? 10);
        return $list;
    }

    public function getQueryFilter($param)
    {
        $params = $this->setQueryDefaultValue($param, [
            'search' => '',
            'menu_id' => '',
            'id_card' => '',
            'contact' => '',
            'status' => -1,
            'feedback_type' => -1,
        ]);

        $filter = [];
        !empty($params['search']) && $filter[] = ["title", "like", "%{$params['search']}%"];
        $params['menu_id'] && $filter[] = ["menu_id", "=", $params['menu_id']];
        $params['id_card'] && $filter[] = ["id_card", "like", "%{$params['id_card']}%"];
        $params['contact'] && $filter[] = ["contact", "like", "%{$params['contact']}%"];
        $params['status'] > -1 && $filter[] = ["status", "=", $params['status']];
        $params['feedback_type'] > -1 && $filter[] = ["feedback_type", "=", $params['feedback_type']];

        if (!empty($params['create_time'])) {
            $times = between_time($params['create_time']);
            $filter[] = ['create_time', '>=', $times['start_time']];
            $filter[] = ['create_time', '<', $times['end_time'] + 86400];
        }
        return $filter;
    }

    /**
     * 增加记录
     * @param array $data
     * @return bool
     */
    public function add($data, $admin)
    {
//        $data['admin_user_id'] = $admin['user']['admin_user_id'];
        return $this->save($data);
    }

    /**
     * 更新记录
     * @param array $data
     * @return bool
     */
    public function edit(array $data)
    {
        return $this->save($data);
    }


    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 批量软删除
     * @return false|int
     */
    public function batchSetDelete($ids)
    {
        return $this->where($this->getPk(), 'in', $ids)->save(['is_delete' => 1]);
    }
    public function labelProcess($ids)
    {
        return $this->where($this->getPk(), 'in', $ids['ids'])->save(['status' => 1]);
    }
}
